package myapp.camera;

import java.util.ArrayList;

import myapp.DTO.ServiceDTO;
import myapp.DTO.SubItemDTO;
import myapp.googlemap.R;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DetailActivity extends Activity {

	private ListView listView_detail;
	private ArrayAdapter<String> listAdapter ; 
	ServiceDTO serviceItem;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		
		serviceItem =  (ServiceDTO) getIntent().getExtras().get("serviceItem");
		SubItemDTO subItem = (SubItemDTO) getIntent().getExtras().get("subItem");

		
		listView_detail = (ListView)findViewById(R.id.listView_detail);
		
		ArrayList<String> detailList = new ArrayList<String>();  
		if (serviceItem.getName() != null)
			if (serviceItem.getName().length() != 0)
				detailList.add(Html.fromHtml("Name:<br/>\t " + serviceItem.getName()).toString());
		
		if (serviceItem.getAddress() != null)
			if (serviceItem.getAddress().length() != 0)
				detailList.add(Html.fromHtml("Address:<br/>\t" + serviceItem.getAddress()).toString());
		
		if (serviceItem.getPhoneNumber() != null)
			if (serviceItem.getPhoneNumber().replace(" ", "").length() != 0)
				detailList.add(Html.fromHtml("Phone:<br/>\t" + serviceItem.getPhoneNumber()).toString());

		if (serviceItem.getDescription() != null)
			if (serviceItem.getDescription().length() != 0)
				detailList.add(Html.fromHtml("Description:<br/>\t" + serviceItem.getDescription()).toString());

		try
		{
			if (subItem != null)
				detailList.add(Html.fromHtml("Discount:<br/>\t" + subItem.name + "<br/>\t"
						+ subItem.details + "<br/>\t"
						+ "From: " + subItem.dateStart + "<br/>\t"
						+ "To: " + subItem.dateFinish).toString());
		}
		catch (Exception e)
		{			
		}
		
	    listAdapter = new ArrayAdapter<String>(this, R.layout.simple_row, detailList);		
	    listView_detail.setAdapter(listAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detail_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) 
		{
		case R.id.detail_call: 
			String phoneNumber = serviceItem.getPhoneNumber();
			if (phoneNumber == null)
				return true;
			
			phoneNumber = phoneNumber.replace(" ", "").replace(".", "").replace("(", "").replace(")", "").replace("[", "").replace("]", "").replace(",", "");
			if (phoneNumber.length() == 0)
				return true;
			final String[] stringSplit = phoneNumber.split("-");

			
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Selecting a phone number");
			builder.setItems(stringSplit, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent callIntent = new Intent(Intent.ACTION_CALL);
					callIntent.setData(Uri.parse("tel:" + stringSplit[which]));
					startActivity(callIntent);

					dialog.dismiss();
				}
			});
			builder.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			AlertDialog alert = builder.create();
			alert.show();			
			return true;
		}
		return false;
	}
}
